package org.springblade.modules.sga.service.impl;

import com.wfl.uias.common.JsonUtils;
import com.wfl.uias.dto.JITObject;
import com.wfl.uias.http.HttpClientExecutor;
import com.wfl.uias.servlet.JITResponseHandler;
import org.springblade.modules.sga.service.ISgaUiasService;
import org.springblade.modules.sga.vo.SgaJITEntUserVO;
import org.springblade.modules.sga.vo.SgaJITLoginVO;
import org.springblade.modules.sga.vo.SgaJITUserVO;
import org.springblade.modules.sga.vo.SgaJITVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class SgaUiasServiceImpl implements ISgaUiasService {

	@Value("${uias.accessHost}")
	private String accessHost;

	@Value("${uias.absolutePath}")
	private String absolutePath;

	@Value("${uias.UIASFilePath}")
	private String UIASFilePath;

	private final String decrypto = "PKCS1";

	@Override
	public SgaJITLoginVO GetJITLoginUser(String accessToken) {
		String jitObjectText = GetJITText(accessToken);
		if (jitObjectText != null) {
			SgaJITVO jitVO = JsonUtils.textToBean(new SgaJITVO(), jitObjectText);
			switch (jitVO.getUserType()) {
				case "1":
					return GetLoginJITUser(jitObjectText);
				case "2":
					return GetLoginJITEntUser(jitObjectText);
				default:
					return null;
			}
		}
		return null;
	}


	private String GetJITText(String accessToken) {
		/* 获取用户详细信息 */

		String filePath = UIASFilePath;

		if (absolutePath != null && "0".equals(absolutePath)) {
			String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
			filePath = path + UIASFilePath;
		}

		if (accessHost != null && !"".equals(accessHost)) {
			//使用授权码换取token
			final String fullUri = accessHost + "/unity/other_info";
			HttpClientExecutor executor = new HttpClientExecutor(fullUri);
			executor.addRequestParam("access_token", accessToken);

			JITResponseHandler responseHandler = new JITResponseHandler();
			executor.execute(responseHandler, "get", filePath, decrypto);

			JITObject jitObject = responseHandler.getObj();
			System.out.println("10.jitObject=" + jitObject.getText());
			return jitObject.getText();
		}

		return null;
	}

	private SgaJITLoginVO GetLoginJITUser(String jitObjectText) {
		SgaJITUserVO jitObject = JsonUtils.textToBean(new SgaJITUserVO(), jitObjectText);

		SgaJITLoginVO loginVO = new SgaJITLoginVO();
		loginVO.setEntUser(0);

		loginVO.setEntId("");
		loginVO.setEntName("");
		loginVO.setLegalName(jitObject.getName());
		loginVO.setLegalCard(jitObject.getIdCard());

		loginVO.setUnitCode("");
		loginVO.setUserType(Integer.parseInt(jitObject.getUserType()));
		loginVO.setUserName(jitObject.getName());
		loginVO.setUserPhone(jitObject.getMobilePhone());

		return loginVO;
	}

	private SgaJITLoginVO GetLoginJITEntUser(String jitObjectText) {
		SgaJITEntUserVO jitObject = JsonUtils.textToBean(new SgaJITEntUserVO(), jitObjectText);
		SgaJITLoginVO loginVO = new SgaJITLoginVO();
		loginVO.setEntUser(1);

		loginVO.setEntId(jitObject.getEntId());
		loginVO.setEntName(jitObject.getEntName());
		loginVO.setLegalName(jitObject.getLegalName());
		loginVO.setLegalCard(jitObject.getLegalCard());

		loginVO.setUnitCode(jitObject.getEntUnitCode());
		loginVO.setUserType(Integer.parseInt(jitObject.getUserType()));
		loginVO.setUserName(jitObject.getEntName());
		loginVO.setUserPhone(jitObject.getMobilePhone());
		return loginVO;
	}
}
